<?php

/**
 * This is the model class for table "bt_batterys".
 *
 * The followings are the available columns in table 'bt_batterys':
 * @property string $RECORD_ID
 * @property integer $BASE_N
 * @property string $GROUPNAME_V
 * @property integer $BATTERYTYPE_V
 * @property string $BATTERYNUM_N
 * @property string $SETUP_D
 * @property integer $CREATE_USERID
 * @property string $CUR_DATE
 * @property string $DEVICE_V
 * @property string $EVENT_NUM
 * @property string $SETUPADDR_V
 * @property string $BACKLOAD_V
 * @property string $POWERSUPPLY_V
 * @property string $ALARMRED_N
 * @property string $NORMALRED_N
 * @property string $NORMALYELLOW_N
 * @property string $ALARMYELLOW_N
 * @property string $STATUS_N
 * @property string $GREEN
 * @property string $YELLOW
 * @property string $RED
 * @property string $REMARK_V
 */
class BtBatterys extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return BtBatterys the static model class
	 */
	public $id; 
	public $searchQuery = ''; 
	 
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'bt_batterys';
	}

	
	public function setQuery($sql){
		//var_dump($sql);
		$this->searchQuery = $sql;
		//var_dump($this->searchQuery);
	}

	public function getQuery(){
		//var_dump($this->searchQuery);
		return $this->searchQuery;
	}
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
				
			array('RECORD_ID, BASE_N, GROUPNAME_V, BATTERYTYPE_V, MANCODE_V, SETUP_D, BASE_TYPE', 'required'),
			array('RECORD_ID', 'unique'),
			array('RECORD_ID', 'match', 'pattern' => '/^[0-9]{6}+-[0-9]{1,2}$/u','message' => "请参考正确电池组编码格式填写"),
			
			//array('BASE_N, BATTERYTYPE_V, CREATE_USERID', 'numerical', 'integerOnly'=>true),
			//array('RECORD_ID, GROUPNAME_V, SETUPADDR_V, REMARK_V', 'length', 'max'=>255),
			array('BATTERYNUM_N, DEVICE_V, IP_V,  PORT_N, ADDR_N, SETUPADDR_V, REMARK_V', 'safe'),
			//array('DEVICE_V, BACKLOAD_V, POWERSUPPLY_V', 'length', 'max'=>100),
			//array('STATUS_N', 'length', 'max'=>2),
			//array('GREEN, YELLOW, RED', 'length', 'max'=>3),
			//array('SETUP_D ', 'safe'),  
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			//array('id','RECORD_ID, BASE_N, GROUPNAME_V, BATTERYTYPE_V, BATTERYNUM_N, SETUP_D, CREATE_USERID, CUR_DATE, DEVICE_V, IP_V, PORT_N, ADDR_N, EVENT_NUM, SETUPADDR_V, BACKLOAD_V, POWERSUPPLY_V, ALARMRED_N, NORMALRED_N, NORMALYELLOW_N, ALARMYELLOW_N, STATUS_N, GREEN, YELLOW, RED, REMARK_V', 'safe', 'on'=>'search'),
		array('base_search', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'base'=>array(self::BELONGS_TO,'MtsysBase', 'BASE_N'),
				'dictbattery'=>array(self::BELONGS_TO, 'DictBattery', 'BATTERYTYPE_V'),
				'dictmanufacturer'=>array(self::BELONGS_TO, 'DictManufacturer', 'MANCODE_V'),
				'bttype' => array(self::BELONGS_TO, 'MtsysBasetype', 'BASE_TYPE'),
//				'dictbattery'=>array(self::HAS_ONE, 'DictBattery', ''),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'RECORD_ID' => '电池组编码',
			'BASE_TYPE' => '电池组警告条件',
			'BASE_N' => '所属站点',
			'MANCODE_V'=> '制造商',
			'GROUPNAME_V' => '电池组名称',
			'BATTERYTYPE_V' => '电池规格',
			'BATTERYNUM_N' => '电池数量',
			'SETUP_D' => '安装日期',
			//'INSTALL_TIME' => '安装日期',				
			'CREATE_USERID' => '创建人',
			'CUR_DATE' => '采集时间',
			'DEVICE_V' => '设备地址',
			'IP_V' => 'IP地址', 
			'PORT_N' => '端口地址', 
			'ADDR_N' => '传感器地址',
			'EVENT_NUM' => '事件数量',
			'SETUPADDR_V' => '安装地址',
			'BACKLOAD_V' => '后备负载',
			'POWERSUPPLY_V' => '供电情况',
			'ALARMRED_N' => '黄色电池组最大红色单体数',
			'NORMALRED_N' => '绿色电池组最大红色单体数',
			'NORMALYELLOW_N' => '绿色电池组最大黄色单体数',
			'ALARMYELLOW_N' => '黄色电池组最大黄色单体数',
			'STATUS_N' => '电池组状态',
			'GREEN' => '绿色电池数',
			'YELLOW' => '黄色电池数',
			'RED' => '红色电池数',
			'REMARK_V' => '备注',
		);
	}

	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function searchByUserid()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
		$bases;
		$sqlbase;
		//get batterys from userid
		$mUser=User::model()->findByPk(Yii::app()->user->id);
		$unit_level = $mUser->unit_level;
		$unit_id = $mUser->unit_id;
			
//		echo "level=".$unit_level;
		//get right by uid 
		switch ($mUser->unit_level){
			case 4:
				$sqlbase = "select * from mtsys_base where BASE_ID in ( ".$unit_id." )";
 
				break;
			case 3:
				$sqlbase = "select * from mtsys_base where COUNTY_ID = ".$unit_id;
 
				break;
			case 2:
				$sqlbase = "select * from mtsys_base where CITY_ID = ".$unit_id;
			 
				break;
			case 1:
				$sqlbase = "select * from mtsys_base where PROVINCE_ID = ".$unit_id;
			 
				break;
			case 0:
				$sqlbase = "select * from mtsys_base " ;
				 
				 
				break;
			default:
				break;
		}
		 
		$bases = new CSqlDataProvider($sqlbase, array());
 		if($bases != null){
 			$basesData = $bases->getData();
 			$basearray = array();
 			foreach($basesData as $base ){
 				array_push($basearray , $base["BASE_ID"]);
 			}
// 			var_dump($basearray);
 			$criteria->addInCondition('BASE_N',$basearray);
 		}		 
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>array(
						'attributes'=>array(
								'base_search'=>array(
										'asc'=>'BASE_N',
										'desc'=>'BASE_N desc',
								),'STATUS_N', 'BATTERYTYPE_V',  'RECORD_ID',
								
						),
				),
		));
	}
		
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('RECORD_ID',$this->RECORD_ID,true);
		$criteria->compare('BASE_N',$this->BASE_N);
		$criteria->compare('GROUPNAME_V',$this->GROUPNAME_V,true);
		$criteria->compare('BATTERYTYPE_V',$this->BATTERYTYPE_V);
		$criteria->compare('BATTERYNUM_N',$this->BATTERYNUM_N,true);
		$criteria->compare('SETUP_D',$this->SETUP_D,true);
		//$criteria->compare('INSTALL_TIME',$this->SETUP_D,true);		
		$criteria->compare('CREATE_USERID',$this->CREATE_USERID);
		$criteria->compare('CUR_DATE',$this->CUR_DATE,true);
		$criteria->compare('DEVICE_V',$this->DEVICE_V,true);
		$criteria->compare('EVENT_NUM',$this->EVENT_NUM,true);
		$criteria->compare('SETUPADDR_V',$this->SETUPADDR_V,true);
		$criteria->compare('BACKLOAD_V',$this->BACKLOAD_V,true);
		$criteria->compare('POWERSUPPLY_V',$this->POWERSUPPLY_V,true);
		$criteria->compare('ALARMRED_N',$this->ALARMRED_N,true);
		$criteria->compare('NORMALRED_N',$this->NORMALRED_N,true);
		$criteria->compare('NORMALYELLOW_N',$this->NORMALYELLOW_N,true);
		$criteria->compare('ALARMYELLOW_N',$this->ALARMYELLOW_N,true);
		$criteria->compare('STATUS_N',$this->STATUS_N,true);
		$criteria->compare('GREEN',$this->GREEN,true);
		$criteria->compare('YELLOW',$this->YELLOW,true);
		$criteria->compare('RED',$this->RED,true);
		$criteria->compare('REMARK_V',$this->REMARK_V,true);
		$criteria->compare('REMARK_V',$this->REMARK_V,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function searchByBid($bid)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
	
 
		$criteria->compare('BASE_N',$bid);
		//$criteria->with = array( 'base' );
		//$criteria->compare( 'base.BASE_ID', $this->base, true );
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>array(
						'attributes'=>array(
								'base_search'=>array(
										'asc'=>'BASE_N',
										'desc'=>'BASE_N  DESC',
								),'STATUS_N', 'BATTERYTYPE_V',
								 
						),
				),
		));
	}
}